package vip.zhenzicheng.concurrent_program.thread;

import java.util.concurrent.TimeUnit;

/**
 * 守护线程demo
 *
 * @author zhenzicheng
 * @date 2022-06-03 15:56
 */
public class DaemonThreadDemo {

  public static void main(String[] args) throws InterruptedException {
    Thread t = new Thread(() -> {
      System.out.println("我是：" + Thread.currentThread().getName());
      while (true) {

      }
    }, Thread.currentThread().isDaemon() ? "守护线程" : "用户线程");

    t.setDaemon(true); // 由于设置了守护线程，所以Main线程结束后，守护线程自动结束，而不是死循环
    t.start();

    TimeUnit.SECONDS.sleep(3);
    System.out.println(Thread.currentThread().getName() + "\t 主线程---end");
  }
}
